package com.itsitio.registeradmin.dao.impl;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.itsitio.registeradmin.dao.UserDao;
import com.itsitio.registeradmin.dao.base.SpringHibernateDao;
import com.itsitio.registeradmin.model.MilesDetail;
import com.itsitio.registeradmin.model.News;
import com.itsitio.registeradmin.model.Portal;
import com.itsitio.registeradmin.model.User;


@Repository
@Transactional
public class UserDaoImpl extends SpringHibernateDao<User> implements
			UserDao{

	private static final Logger log = LoggerFactory.getLogger(UserDaoImpl.class);
	
	@Autowired
	public UserDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	
	@Override
	protected Class<User> getObjetoManejado() {
			return User.class;
	}
	
	public List<User> getUserListByBranch(Integer branchId) {	

		DetachedCriteria criteria = DetachedCriteria.forClass(getObjetoManejado());
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		criteria.add(Restrictions.eq("branch.id", branchId));
		return  getHibernateTemplate().findByCriteria(criteria);
	}

	public List<User> findUserByFilter(String lastName, String email){
		if(log.isDebugEnabled())
			log.debug("Begin with lastName="+lastName+", email="+email+")");
		
		List<User> result = new LinkedList<User>();
		
		if ((lastName != null && lastName.trim().length()> 0) || (email != null && email.trim().length()> 0)) {
			DetachedCriteria criteria = DetachedCriteria.forClass(getObjetoManejado());
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			
			if (lastName != null && lastName.trim().length()> 0)  {
				criteria.add(Restrictions.like("lastName", email, MatchMode.START));
			}
			
			if (email != null && email.trim().length()> 0) {
				criteria.add(Restrictions.like("email", email, MatchMode.ANYWHERE));				
			}
			
			result = getHibernateTemplate().findByCriteria(criteria);			
		}
		
		if (log.isDebugEnabled()) {
			if (result != null)
				log.debug("User result.size()=" + result.size());
			else
				log.debug("User result=null");
		}		
						
		return  result;
	}


	@Override
	public List<Portal> getUserPortals(User user) {
		List<Portal> portals = user.getPortales();
		if (!Hibernate.isInitialized(portals)) {
			getSession().lock(user, LockMode.NONE);
			Hibernate.initialize(portals);
		}
		return portals;
	}


	@Override
	public List<News> getUserNews(User user) {
		List<News> news = user.getNews();
		if (!Hibernate.isInitialized(news)) {
			getSession().lock(user, LockMode.NONE);
			Hibernate.initialize(news);
		}
		return news;
	}


	@Override
	public List<MilesDetail> getUserMilesDetail(User user) {
		List<MilesDetail> milesDetailList = user.getMilesDetailList();
		if (!Hibernate.isInitialized(milesDetailList)) {
			getSession().lock(user, LockMode.NONE);
			Hibernate.initialize(milesDetailList);
		}
		return milesDetailList;
	}




}
